package com.jcfk.system.service;

import com.jcfk.system.domain.dto.OaRemindUserDTO;
import com.jcfk.system.domain.po.OaRemindPlan;
import com.jcfk.system.domain.po.OaRemindUser;
import com.jcfk.system.domain.query.OaRemindPlanQuery;
import com.jcfk.system.domain.dto.OaRemindPlanDTO;
import com.jcfk.system.dao.OaRemindPlanDao;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

import com.jcfk.common.core.utils.IdUtils;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * 周报日报提醒
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-04-12
 */
@Service
public class OaRemindPlanService extends BaseService<OaRemindPlanDao, OaRemindPlan> {

    @Autowired
    private OaRemindUserService oaRemindUserService;


    /**
     * OaRemindPlan分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<OaRemindPlanDTO> queryByPage(DataPage<OaRemindPlanDTO> page, OaRemindPlanQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断OaRemindPlan是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(OaRemindPlanDTO dto) {
        QueryWrapper<OaRemindPlan> queryWrapper = new QueryWrapper<>();

        return RetResult.ok();
    }

    /**
     * 插入周报日报提醒
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(OaRemindPlanDTO dto) {
        dto.setId(null);
        // 保存
        OaRemindPlan item = new OaRemindPlan();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);

        List<OaRemindUser> users = new ArrayList<>();

        // 日报人
        for (Map<String, String> dailyUser : dto.getDailyUsers()) {
            OaRemindUser oaRemindUser = new OaRemindUser();
            oaRemindUser.setUserId(dailyUser.get("id"));
            oaRemindUser.setUserName(dailyUser.get("userName"));
            oaRemindUser.setType("1");
            oaRemindUser.setRemindId(item.getId());
            oaRemindUser.setRemindType("1");
            oaRemindUserService.setDefaultValue(oaRemindUser);
            users.add(oaRemindUser);
        }

        if (!StringUtils.isEmpty(dto.getDailyOrganizationId())) {
            OaRemindUser oaRemindUser = new OaRemindUser();
            oaRemindUser.setOrganizationId(dto.getDailyOrganizationId());
            oaRemindUser.setType("1");
            oaRemindUser.setRemindId(item.getId());
            oaRemindUser.setRemindType("3");
            oaRemindUserService.setDefaultValue(oaRemindUser);
            users.add(oaRemindUser);
        }

        for (String dailyGroupId : dto.getDailyGroupIds()) {
            OaRemindUser oaRemindUser = new OaRemindUser();
            oaRemindUser.setGroupId(dailyGroupId);
            oaRemindUser.setType("1");
            oaRemindUser.setRemindId(item.getId());
            oaRemindUser.setRemindType("2");
            oaRemindUserService.setDefaultValue(oaRemindUser);
            users.add(oaRemindUser);
        }


        // 周报人
        for (Map<String, String> dailyUser : dto.getWeekUsers()) {
            OaRemindUser oaRemindUser = new OaRemindUser();
            oaRemindUser.setUserId(dailyUser.get("id"));
            oaRemindUser.setUserName(dailyUser.get("userName"));
            oaRemindUser.setType("2");
            oaRemindUser.setRemindId(item.getId());
            oaRemindUser.setRemindType("1");
            oaRemindUserService.setDefaultValue(oaRemindUser);
            users.add(oaRemindUser);
        }

        if (!StringUtils.isEmpty(dto.getWeekOrganizationId())) {
            OaRemindUser oaRemindUser = new OaRemindUser();
            oaRemindUser.setOrganizationId(dto.getWeekOrganizationId());
            oaRemindUser.setType("2");
            oaRemindUser.setRemindId(item.getId());
            oaRemindUser.setRemindType("3");
            oaRemindUserService.setDefaultValue(oaRemindUser);
            users.add(oaRemindUser);
        }

        for (String dailyGroupId : dto.getWeekGroupIds()) {
            OaRemindUser oaRemindUser = new OaRemindUser();
            oaRemindUser.setGroupId(dailyGroupId);
            oaRemindUser.setType("2");
            oaRemindUser.setRemindId(item.getId());
            oaRemindUser.setRemindType("2");
            oaRemindUserService.setDefaultValue(oaRemindUser);
            users.add(oaRemindUser);
        }

        if (users.size() > 0) oaRemindUserService.saveBigBatch(users);
    }

    /**
     * 更新周报日报提醒
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(OaRemindPlanDTO dto) {

        // 保存
        OaRemindPlan item = new OaRemindPlan();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }


    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(OaRemindPlan item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());
        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取周报日报提醒信息
     *
     * @return StandardCodeDTO
     */
    public OaRemindPlanDTO get() {
        // 查询数据
        QueryWrapper<OaRemindPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time").last("LIMIT 1");
        OaRemindPlan item = this.baseMapper.selectOne(queryWrapper);
        OaRemindPlanDTO dto = new OaRemindPlanDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        QueryWrapper<OaRemindUser> oaRemindUserQueryWrapper = new QueryWrapper<>();
        oaRemindUserQueryWrapper.lambda().eq(OaRemindUser::getRemindId, item.getId());

        List<OaRemindUser> list = oaRemindUserService.list(oaRemindUserQueryWrapper);

        for (OaRemindUser oaRemindUser : list) {
            // 日报
            if (oaRemindUser.getType().equals("1")) {
                if (oaRemindUser.getRemindType().equals("1")) {
                    Map<String, String> map = new HashMap<>();
                    map.put("id", oaRemindUser.getUserId());
                    map.put("userName", oaRemindUser.getUserName());
                    dto.getDailyUsers().add(map);
                }

                if (oaRemindUser.getRemindType().equals("2")) {
                    dto.getDailyGroupIds().add(oaRemindUser.getGroupId());
                }

                if (oaRemindUser.getRemindType().equals("3")) {
                    dto.setDailyOrganizationId(oaRemindUser.getOrganizationId());
                }
            } else {
                // 周报
                if (oaRemindUser.getRemindType().equals("1")) {
                    Map<String, String> map = new HashMap<>();
                    map.put("id", oaRemindUser.getUserId());
                    map.put("userName", oaRemindUser.getUserName());
                    dto.getWeekUsers().add(map);
                }

                if (oaRemindUser.getRemindType().equals("2")) {
                    dto.getWeekGroupIds().add(oaRemindUser.getGroupId());
                }

                if (oaRemindUser.getRemindType().equals("3")) {
                    dto.setWeekOrganizationId(oaRemindUser.getOrganizationId());
                }
            }
        }

        return dto;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<OaRemindPlanDTO>
     */
    public List<OaRemindPlanDTO> getList(OaRemindPlanQuery query) {
        // 查询条件
        QueryWrapper<OaRemindPlan> qw = new QueryWrapper<>();
        // 查询数据
        List<OaRemindPlan> list = super.list(qw);

        return convertList(list, OaRemindPlanDTO.class);
    }
}

